package com.imessage.action.device.virtual;


import com.bean.DeviceClient;
import com.bean.DeviceVirtual;
import com.imessage.comm.Constants;
import com.imessage.type.BusinessTaskCmd;
import com.imessage.vo.ClientVo;
import com.imessage.vo.VirtualVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户
 */
@Slf4j
@RestController
@RequestMapping("/admin/virtual")
public class VirtualAction extends VirtualBaseAction {


    /**
     * 客户列表数据和搜索
     */
    @RequestMapping(value = "")
    public Object run(VirtualVo virtualVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, virtualVo);
        Page<DeviceVirtual> page = baseService.findPage(
                DeviceVirtual.class, expressionList, null,
                getOrder(virtualVo.getOrders()), virtualVo.getPageCurrent(),
                virtualVo.getPageSize());
        return page;
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    public Object add(VirtualVo virtualVo) {
        DeviceVirtual virtual = new DeviceVirtual();
        setVirtual(virtualVo, virtual);
        int state = baseService.saveObject(virtual);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify")
    public Object modify(VirtualVo virtualVo) {
        DeviceVirtual virtual = baseService.findById(DeviceVirtual.class, virtualVo.getId());
        setVirtual(virtualVo, virtual);
        int state = baseService.updateObject(virtual);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return virtual;
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            DeviceVirtual virtual = baseService.findById(DeviceVirtual.class, id);
            taskService.clearBusinessTask(virtual);
            state = baseService.delObject(virtual);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(@RequestParam(name = "req_id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        DeviceVirtual virtual = new DeviceVirtual();
        DeviceClient client = new DeviceClient();
        if (StringUtils.isNotBlank(id)) {
            virtual = baseService.findById(DeviceVirtual.class, id);
            client = baseService.findById(DeviceClient.class, virtual.getDeviceClient().getId());
            modelAndView.setViewName("admin/device/virtual/modify");
        } else {
            modelAndView.setViewName("admin/device/virtual/add");
        }
        setModelAndView(modelAndView, virtual, client);
        return modelAndView;
    }


    @RequestMapping(value = "state")
    public Object state(@RequestParam("state") String state,
                        @RequestParam("id") long id) {
        DeviceVirtual deviceVirtual= baseService.findById(DeviceVirtual.class, id);
        if(deviceVirtual==null){
            HttpJson httpJson=new HttpJson(HttpJson.STATE_FAIL, "虚拟机不存在", false, "");
            return httpJson;
        }
        BusinessTaskCmd businessTaskCmd=null;
        if(state.equals("shutdown")){
            businessTaskCmd=BusinessTaskCmd.shutdown_virtual;
        }
        if(state.equals("start")){
            businessTaskCmd=BusinessTaskCmd.start_virtual;
        }
        if(state.equals("del")){
            businessTaskCmd=BusinessTaskCmd.del_virtual;
        }
        if(state.equals("restart")){
            businessTaskCmd=BusinessTaskCmd.restart_virtual;
        }
        taskService.saveBusinessTaskFromList(deviceVirtual,businessTaskCmd );
        HttpJson httpJson=new HttpJson(HttpJson.STATE_SUCCESS, "操作成功", false, "");
        return httpJson;
    }


}
